package com.liuqm.controller;


import com.liuqm.service.ICzktOrderLineService;
import com.liuqm.service.ICzktOrderService;
import org.liuqm.client.ActivitiyClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * <p>
 * 订单表 控制器
 * </p>
 *
 * @author liuqiaomiao
 * @ClassName CzktOrderController
 * @since 2024-10-21
 */
@RestController
@RequestMapping("/order")
public class CzktOrderController {

    @Autowired
    private ICzktOrderService czktOrderService;

//    @Autowired
//    private RestTemplate restTemplate;

    @Autowired
    private ActivitiyClient activitiyClient;


    @Autowired
    private ICzktOrderLineService czktOrderLineService;

    /**
     * 秒杀方法
     *
     * @return
     */
    @RequestMapping(value = "/add")
    public Object confrimOrder(Integer userId, Integer skuId, Integer activityId) {


        Map o=activitiyClient.getCzktActivityById(activityId);
        System.out.println(o.get("stockNum"));
//        //1、判断是否有库存活动是否有效
//        Map o = restTemplate.getForObject("http://localhost:8090/activity/selectById?activityId="+activityId, LinkedHashMap.class);
//        System.out.println(o.get("stockNum"));
//        CzktActivity activity = czktActivityService.getById(activityId);
//        if (activity.getStockNum() == 0) {
//            return "库存不足！";
//        }
////        //2、 判断用户是否有参与了这个活动
////        long buyCount = czktOrderService.count(new LambdaQueryWrapper<CzktOrder>()
////                .eq(CzktOrder::getActivityId, activityId)
////                .eq(CzktOrder::getBuyerId, userId));
////        if (buyCount > 0) {
////            return "您已参加了该活动！";
////        }
//
//        //3、下单  （添加订单表数据 ）
//
//        CzktOrder czktOrder = new CzktOrder();
//        czktOrder.setActivityId(activityId);
//        czktOrder.setBuyerId(userId);
//        czktOrder.setOrderCode(UUID.randomUUID().toString());
//        czktOrder.setOrderAmount(activity.getActivityPrice());
//        czktOrder.setPostTime(new Date());
//        boolean addResult = czktOrderService.save(czktOrder);
//        if (!addResult) {
//            return "网络异常1！";
//        }
//
//        CzktOrderLine czktOrderLine = new CzktOrderLine();
//        czktOrderLine.setBuyerNum(1);
//        czktOrderLine.setOrderId(czktOrder.getId());
//        czktOrderLine.setSkuId(skuId);
//        czktOrderLine.setProductAmount(activity.getActivityPrice());
//        addResult = czktOrderLineService.save(czktOrderLine);
//        if (!addResult) {
//            return "网络异常2！";
//        }
//        //4、扣减活动库存
////        activity.setStockNum(activity.getStockNum() - 1);
////       使用Mysql悲观锁实现扣减库存
//        addResult = czktActivityService.updateActivity(activityId);
//        if (!addResult) {
//            return "网络异常3！";
//        }
        return "抢购成功";

    }


}
